import { $toast } from '../../utils/utils';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    monthSigin:{
      type:Array,
      default:[],
      observer:'updataSG'
    },
    monthJfData:{
      type:Array,
      default:[],
      observer:'updataJF'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    weekList: ['日', '一', '二', '三', '四', '五', '六'],
    monthShow: true,
    year: 0,
    month: 0,
    day: 0,
    week: '',
    nowDay: '',
    selectDay: '',
    everyDay: [],
    JfData:[]
  },

  attached() {
    this.setData({
      year:Number(this.getNowTime(new Date(), 2).slice(0, 4)),
      month:Number(this.getNowTime(new Date(), 2).slice(5, 7)),
      day: Number(this.getNowTime(new Date(), 2).slice(8, 10)),
      nowDay: this.getNowTime(new Date()),
      selectDay: this.getNowTime(new Date())
    })
    if(wx.getSystemInfoSync().platform == 'ios'){
      this.getEveryDay(this.data.year + '/' + this.data.month + '/' + this.data.day);
    }else{
      console.log('年月',this.data.year + '-' + this.data.month)
      this.getEveryDay(this.data.year + '-' + this.data.month);
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    updataSG(upArr){
      let monArr = this.data.everyDay;
      upArr.forEach(item=>{
        monArr.forEach(mi=>{
          if(item.day==mi.day){
            mi.isSigin = true;
          }
        })
      })
      this.setData({
        everyDay:monArr
      })
      console.log('----------',this.data.everyDay)
    },

    updataJF(updata) {
      console.log('实时监听该数据',updata)
      this.setData({
        JfData:updata
      })
      let monArr = this.data.everyDay;
      monArr.forEach(item=>{
        if(item.date){
          let filterItem = updata.filter(f=>f.date == item.day)
          item.points = filterItem[0].points
        }
      })
      this.setData({
        everyDay:monArr
      })
    },
    // 获取当前日期
    getNowTime(time, type) {
      var date = time,
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
        minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
        second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        month >= 1 && month <= 9 ? (month = "0" + month) : "";
        day >= 0 && day <= 9 ? (day = "0" + day) : "";
        if (type == 1) {
          if(uni.getSystemInfoSync().platform == 'ios'){
            var timer = 
              year + '/' + month + '/' + day + 
              ' ' + hour + ':' + minute + ':' + second;
          }else{
            var timer = year + '-' + month + '-' + day + 
            ' ' + hour + ':' + minute + ':' + second;
          }
        } else {
          var timer = year + '/' + month + '/' + day;
        }
      
      return timer;
    },

    getEveryDay(data) {
      let date = new Date(data);
      console.log('getEveryDay',date)
      let month = date.getMonth();
      //设置月份
      date.setMonth(month + 1);
      //设置一个月的某一天-这里设置为零则取到的日期中的天就会是当月的最后一天（比如，二月份就是28或29，其他月份就是30或31），方便下边的循环
      date.setDate(0);
      let dayArry = [];
      let day = date.getDate(); //获取当前月最后一天是几号
      for (let i = 1; i <= day; i++) {
        date.setDate(i); //如果只获取当前选择月份的每一天，则不需要date.setDate(i)只需dayArry.push(i)即可，其中date.setDate(i)是设置当前月份的每一天
        dayArry.push({ day: i, week: this.getWeekday(date.getDay()), date: this.getNowTime(date) }); //选中月份的每一天和当天是星期几
      }
      
      this.setData({
        everyDay: dayArry
      })
      console.log('everyDay~~~~~~~',this.data.everyDay)
      this.resetDay();
    },

    getWeekday(day) {
      return ['日', '一', '二', '三', '四', '五', '六'][day];
    },

    //重置日期与顶部周期相对应
    resetDay() {
      let arr = this.data.weekList;
      let everyDay = this.data.everyDay;
      let w = everyDay[0]?.week;
      arr.forEach((v, index) => {
        if (v == w) {
          let id = index;
          if (id != 0) {
            for (let i = 0; i < id; i++) {
              everyDay.unshift({ day: '', week: '' });
            }
          }
        }
      });
      this.setData({
        everyDay:everyDay
      })
    },
      
    // 切换上一周月、下一周月
    preNextDate(e){
      let next = e.currentTarget.dataset.next
      if(this.data.monthShow){
        if(next == 1){
          let nowM = Number(this.getNowTime(new Date(), 2).slice(5, 7));
          if(this.data.month==nowM){
            $toast('暂时不可查看下一个月');
            return
          }
          // 下一月
          let month = this.data.month;
          let year = this.data.year;
          month += 1;
          this.setData({
            month:month
          })
          year +=1;
          if (this.month > 12) {
            this.setData({
              year:year,
              month:1
            })
          }
          if(wx.getSystemInfoSync().platform == 'ios'){
            this.getEveryDay(this.data.year + '/' + this.data.month + '/' + 1);
          }else{
            this.getEveryDay(this.data.year + '-' + this.data.month);
          }
          let m = this.data.month<10?'0'+this.data.month:this.data.month
          let ym = this.data.year+'-'+m
          this.triggerEvent('changeMonth',{ym:ym})
        }else{
          // 上一月
          let month = this.data.month;
          let year = this.data.year;
          month -= 1;
          this.setData({
            month:month
          })
          if (this.data.month == 0) {
            year -= 1;
            this.setData({
              month:12,
              year:year
            })
          }
          
          if(wx.getSystemInfoSync().platform == 'ios'){
            this.getEveryDay(this.data.year + '/' + this.data.month + '/' + 1);
          }else{
            this.getEveryDay(this.data.year + '-' + this.data.month);
          }

          let m = this.data.month<10?'0'+this.data.month:this.data.month
          let ym = this.data.year+'-'+m
          this.triggerEvent('changeMonth',{ym:ym})
        }
      }
    },
    // 选中日期
    changeDate(e){
      console.log('----------------')
      let item = e.currentTarget.dataset.item;
      let day = item.date.split('/').join('-');
      this.triggerEvent("handleSiginin", {day});
    },
  }
})
